package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.merchanth5.service.LiveGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/livestream")
public class LiveStreamController {

    @Autowired
    private LiveGoodsService liveGoodsService;

    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    //判断是否已开通直播
    @GetMapping("/ifopenlive")
    public Object ifOpenLive(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        String customerId = orgId;
        int type = 1;
        if (ifSales) {
            // 联盟商家
            type = 2;
            customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("customerId", customerId);
        param.put("orgId", orgId);
        param.put("type", type);
        Integer count = liveGoodsService.ifOpenLive(param);
        Map<String, Object> result = new HashMap<>();
        result.put("ifOpenLive", count > 0 ? 1 : 0);
        return result;
    }

    //开通直播
    @GetMapping("/openstream")
    public void openStream(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String userCode = userinfo.getAdminCode();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        String customerId = orgId;
        String bindPhone = "";
        String nickName = "";
        int type = 1;
        if (ifSales) {
            // 联盟商家
            type = 2;
            customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            bindPhone = "mbl"+liveGoodsService.getBindPhoneByCustomerId(customerId);
            nickName = liveGoodsService.getNickNameByCustomerId(customerId);
            nickName = StringUtils.isEmpty(nickName) ? "手机尾号" + bindPhone.substring(bindPhone.length() - 4) + "用户" : nickName;
        } else {
            // 大麦
            bindPhone = userCode;
            nickName = merchantInfoService.getOrgName(orgId);
        }

        // 开通 直播间
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", customerId);
        params.put("orgId", orgId);
        params.put("bindPhone", bindPhone);
        params.put("nickName", nickName);
        //
        params.put("logoUrl", "https://incloud.linker.cc/StepLink/202004/10/14/cXtzK2rdM202004101430.png");
        params.put("iconUrl", "https://incloud.linker.cc/StepLink/202004/10/14/1SLqvVhb3AuZCdscY5vgfrLos2020041014147.png");
        //类型1机构2商家
        params.put("type", type);
        liveGoodsService.openingStream(params);
    }

    //直播列表
    @GetMapping("/livestreamlist")
    public PageInfo liveStreamList(
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            HttpServletRequest request
    ) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        String customerId = orgId;
        if (ifSales) {
            // 联盟商家
            customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("orgId", orgId);
        param.put("customerId", customerId);
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(liveGoodsService.getStudiolist(param));
    }

    //新增直播和修改直播

    /**
     * studioId 修改时传
     * params : account 手机号
     * params : iconUrl 头像
     * params : studioName 直播间名称
     * params : anchorDes 主持人简介
     * params : nickname 主持人昵称
     * params : type
     * params : orgId
     * params : customerId
     * @param params
     * @param session
     */
    @PostMapping("/savelivestream")
    public void saveLiveStream(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        String customerId = orgId;
        int type = 1;
        if (ifSales) {
            // 联盟商家
            type = 2;
            customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
        }
        params.put("customerId", customerId);
        params.put("orgId", orgId);
        params.put("type", type);
        // 商家认证
        Integer checkStatus = liveGoodsService.getCheckStatus(params);
        if (null == checkStatus || checkStatus != 2) {
            throw GlobalException.build("商家还未认证！");
        }
        liveGoodsService.saveStudio(params);
    }

    @GetMapping("/dellivestream")
    public void delLiveStream(String studioId,HttpServletRequest request)
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        String customerId = orgId;
        int type = 1;
        if (ifSales) {
            // 联盟商家
            type = 2;
            customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
        }
        Map<String,Object> params = new HashMap<>();
        params.put("customerId", customerId);
        params.put("orgId", orgId);
        params.put("type", type);
        params.put("studioId", studioId);
        liveGoodsService.delLiveStream(params);
    }
}
